#!/usr/bin/env python3

import os
import re

ROOT_DIR = ".."
RELPATH_README = "README.md"
ABSPATH_README = "docs/README-abspath.md"


def make_abspath_readme():
    os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), ROOT_DIR))
    relpath_dir = os.path.dirname(RELPATH_README) + "/"
    with open(RELPATH_README, "r", encoding="utf-8") as fh:
        text = fh.read()
        text = re.sub(
            r'(\]\(|href=")(?!http)',
            r"\1https://github.com/kitao/pyxel/blob/main/" + relpath_dir,
            text,
        )
        text = re.sub(
            r'(src=")',
            r"\1https://raw.githubusercontent.com/kitao/pyxel/main/" + relpath_dir,
            text,
        )
    with open(ABSPATH_README, "w", encoding="utf-8") as fh:
        fh.write(text)


if __name__ == "__main__":
    make_abspath_readme()
